package cn.promonkey.api.relation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.promonkey.core.dto.UserDto;
import cn.promonkey.core.response.ResultMsg;
import cn.promonkey.core.toolkit.QueryGenerator;
import cn.promonkey.core.toolkit.UserInfoUtils;
import cn.promonkey.api.group.entity.Group;
import cn.promonkey.api.group.service.GroupService;
import cn.promonkey.api.relation.entity.Relation;
import cn.promonkey.api.relation.service.RelationService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;

/** @author <no value> */
@Slf4j
@RestController
@RequestMapping("/relation/")
public class RelationController {
  final RelationService relationService;
  final GroupService groupService;

  public RelationController(RelationService relationService,GroupService groupService) {
    this.relationService = relationService;
    this.groupService = groupService;
  }

  @RequestMapping("getPage")
  public String getPage(
      Relation relation,
      @RequestParam(name = "page", defaultValue = "1") Integer page,
      @RequestParam(name = "size", defaultValue = "10") Integer size,
      HttpServletRequest request) {
    QueryWrapper<Relation> queryWrapper = QueryGenerator.initQueryWrapper(relation, request.getParameterMap());
    Page<Relation> pageable = new Page<>(page, size);
    IPage<Relation> pageList = relationService.page(pageable, queryWrapper);
    return ResultMsg.success(pageList);
  }

  @RequestMapping(value = "save", method = RequestMethod.POST)
  public String save(@RequestBody Relation relation) {
    return ResultMsg.success(relationService.save(relation));
  }

  @RequestMapping(value = "edit", method = RequestMethod.POST)
  public String edit(@RequestBody Relation relation) {
    return ResultMsg.success(relationService.updateById(relation));
  }

  @RequestMapping(value = "get")
  public String get(@RequestParam(name = "id") Long id) {
    return ResultMsg.success(relationService.getById(id));
  }

  @RequestMapping(value = "delete", method = RequestMethod.POST)
  public String delete(@RequestBody Long id) {
    return ResultMsg.success(relationService.removeById(id));
  }
  
  @RequestMapping(value = "saveUser", method = RequestMethod.POST)
  public String saveUser(@RequestBody Relation relation) {
	  UserDto user = UserInfoUtils.getUserInfo();
	  QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
	  queryWrapper.eq("user_id", relation.getUserId());
	  relationService.remove(queryWrapper);
	  List<Relation> list = new ArrayList<>();
	  for(Long id:relation.getCheckList()) {
		  Relation one = new Relation();
		  one.setBusinessId(user.getBusinessId());
		  one.setGroupId(id);
		  //修复 授权不写入 systemid的bug
		  Group group = groupService.getById(id);
		  one.setSystemId(group.getSystemId());
		  one.setUserId(relation.getUserId());
		  list.add(one);
	  }
    return ResultMsg.success(relationService.saveBatch(list));
  }
  
  @RequestMapping(value = "saveGroup", method = RequestMethod.POST)
  public String saveGroup(@RequestBody Relation relation) {
	  UserDto user = UserInfoUtils.getUserInfo();
	  QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
	  queryWrapper.eq("group_id", relation.getGroupId());
	  relationService.remove(queryWrapper);
	  List<Relation> list = new ArrayList<>();
	  for(Long id:relation.getCheckList()) {
		  Relation one = new Relation();
		  one.setBusinessId(user.getBusinessId());
		  one.setGroupId(relation.getGroupId());
		  one.setSystemId(relation.getSystemId());
		  one.setUserId(id);
		  list.add(one);
	  }
    return ResultMsg.success(relationService.saveBatch(list));
  }
}
